
/**
 * Level
 * Template of an instance level
 **/
var Level = function() {

	this.sprites = {
		background : IM.getInstance('img/background/sky')
	};

	this.clouds = [];
	this.ground = null;
	this.conf = null;

	this.w = WIDTH;
	this.h = HEIGHT;

	this.addCloud = function() {
		var cloud = this.conf.sprites.clouds.elements.pickup();
		cloud.sprite = IM.getInstance(getIMName(cloud.img));
		cloud.sprite.animation = new IIG.Animation(cloud.animation);
		cloud.x = rand(0, WIDTH*2);
		cloud.y = 0;
		cloud.speed = rand(1, 5);
		this.clouds.push(cloud);
	}

	this.addGround = function() {
		var ground = this.conf.sprites.grounds.elements.pickup();
		ground.sprite = IM.getInstance(getIMName(ground.img));
		ground.sprite.animation = new IIG.Animation(ground.animation);
		ground.x = 0;
		ground.y = PlayerConf.coords.y + PlayerConf.bounds.h - ground.offsetY;
		this.ground = ground;
	}

	this.init = function(name) {
		this.conf = LevelConf[name];
		for(var iC = 0, c = this.conf.cClouds; iC < c; iC++) {
			this.addCloud();
		}
		this.addGround();
	}

	this.destroy = function() {
		this.clouds = [];
	}

	this.update = function() {
		for(var iC = 0, c = this.clouds.length; iC < c; iC++) {
			this.clouds[iC].x -= this.clouds[iC].speed;
			if (this.clouds[iC].x + this.clouds[iC].sprite.animation.sWidth < 0) {
				// this.addCloud();
			}
		}
	}

	this.render = function() {
		IM.drawImage(CTX, this.sprites.background, 0, 0);

		for(var iC = 0, c = this.clouds.length; iC < c; iC++) {
			var s = this.clouds[iC].sprite;
			CTX.drawImage(
			  s.data, // image resource
			  s.animation.sx, // current 'x' position of the sprite canvas area
			  s.animation.sy, // current 'y' position of the sprite canvas area
			  s.animation.sWidth, // width of the sprite canvas area
			  s.animation.sHeight, // height of the sprite canvas area
			  this.clouds[iC].x,
			  this.clouds[iC].y,
			  s.animation.sWidth,
			  s.animation.sHeight
			);
		}

		IM.drawImage(CTX, this.ground.sprite, this.ground.x, this.ground.y);
	}

	this.getGround = function() {
		return this.ground.y + this.ground.offsetY;
	}

};